package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.GoodsCountBO;
import com.junzhiit.nengji.bo.HomeHeaderInfoBO;
import com.junzhiit.nengji.bo.HomeSituationBO;
import com.junzhiit.nengji.bo.HomeStatisticsBO;
import com.junzhiit.nengji.bo.HomeTableBO;
import com.junzhiit.nengji.bo.NoticeBO;
import com.junzhiit.nengji.bo.OrdersCountBO;
import com.junzhiit.nengji.bo.OrdersMoneyBO;
import com.junzhiit.nengji.cms.service.HomeService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.SystemUserNotFoundException;
import com.junzhiit.nengji.cms.web.vo.HomeTableVO;
import com.junzhiit.nengji.cms.web.vo.OrdersInfoVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.HomeDAO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.PayHistoryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SystemUserDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.SystemUser;

@Service
public class HomeServiceImpl extends BaseServiceImpl implements HomeService{
	
	@Autowired
	private HomeDAO homeDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private PayHistoryDAO payHistoryDAO;
	
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private SystemUserDAO systemUserDAO;
	
	@Autowired
	private ChainDAO chainDAO;
	
	@Autowired
	private OrdersDAO ordersDAO;

	@Override
	public ResultVO getheaderInfo(String sessionId){
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		HomeSituationBO data=homeDAO.getheaderInfo(shop.getId());
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO queryOrdersInfo(String sessionId,Integer year,Integer month){
		ResultVO result=new ResultVO();
		
		if(year==null||month==null){
			return result;
		}
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		List<OrdersMoneyBO> os=homeDAO.queryOrdersMoney(shop.getId(),year,month);
		
		Calendar cl=Calendar.getInstance();
		cl.set(Calendar.MONTH,month-1);
		int daysCount=cl.getActualMaximum(Calendar.DATE);
		
//		Date date=DateUtil.getCurrentDate();
//		int nowYear=DateUtil.getYear(date);
//		int nowMonth=DateUtil.getMonth(date);
//		int nowDay=DateUtil.getDayOfMonth(date);
		
//		daysCount=((nowYear==year.intValue())&&((nowMonth+1)==month.intValue()))?nowDay:daysCount;
//		daysCount=DateUtil.getDayOfMonth(date);
		
		List<String> item=new ArrayList<String>();
		List<Double> moneys=new ArrayList<Double>();
		Map<Integer, OrdersMoneyBO> osmaps=os.stream().collect(Collectors.toMap(OrdersMoneyBO::getDay, ordersMoneyBO -> ordersMoneyBO));
		
		List<OrdersCountBO> ob=homeDAO.queryOrdersCount(shop.getId(),year,month);
		List<Integer> counts=new ArrayList<Integer>();
		Map<Integer, OrdersCountBO> obmaps=ob.stream().collect(Collectors.toMap(OrdersCountBO::getDay, ordersCountBO -> ordersCountBO));
		
		String prefixStr=String.valueOf(year)+"-"+String.valueOf(month)+"-";
		for(int i=1;i<=daysCount;i++){
			item.add(prefixStr+String.valueOf(i));
			
			if(osmaps.containsKey(i)){
				Double money=osmaps.get(i).getMoney();
				moneys.add(money);
			}else{
				moneys.add(0.00);
			}
			
			if(obmaps.containsKey(i)){
				int count=obmaps.get(i).getCount();
				counts.add(count);
			}else{
				counts.add(0);
			}
		}
		
		OrdersInfoVO ordersInfoVO=new OrdersInfoVO();
		
		List<Object> data=new ArrayList<Object>();
		data.add(moneys);
		data.add(counts);
		
		ordersInfoVO.setData(data);
		ordersInfoVO.setItem(item);
		
		result.setData(ordersInfoVO);
		
		return result;
	}

	@Override
	public ResultVO getHeaderStatistics(String sessionId) {
		ResultVO result=new ResultVO();
		HomeStatisticsBO homeStatisticsBO=new HomeStatisticsBO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		List<SystemUser> systemUser = systemUserDAO.getListBySystemId(systemUserSessionVO.getSystemUserId());
		if (systemUser == null) {
			throw new SystemUserNotFoundException("该系统用户不存在");
		}
		homeStatisticsBO.setTotalUser(userDAO.getCountByUser());
		homeStatisticsBO.setTotalShop(shopDAO.getCountByShop());
		homeStatisticsBO.setTodayShopApproval(shopDAO.getCountByShopApproval());
		homeStatisticsBO.setTodayGoodApproval(goodsDAO.getCountByGoodsApproval());
		
		result.setData(homeStatisticsBO);
		return result;
	}
	
	@Override
	public ResultVO queryAllOrdersInfo(String sessionId,Integer year,Integer month){
		ResultVO result=new ResultVO();
		
		if(year==null||month==null){
			return result;
		}
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		List<SystemUser> systemUser = systemUserDAO.getListBySystemId(systemUserSessionVO.getSystemUserId());
		if (systemUser == null) {
			throw new SystemUserNotFoundException("该系统用户不存在");
		}
		
		List<OrdersMoneyBO> os=homeDAO.queryAllOrdersMoney(year,month);
		
		Calendar cl=Calendar.getInstance();
		cl.set(Calendar.MONTH,month-1);
		int daysCount=cl.getActualMaximum(Calendar.DATE);
		
		List<String> item=new ArrayList<String>();
		List<Double> moneys=new ArrayList<Double>();
		Map<Integer, OrdersMoneyBO> osmaps=os.stream().collect(Collectors.toMap(OrdersMoneyBO::getDay, ordersMoneyBO -> ordersMoneyBO));
		
		List<OrdersCountBO> ob=homeDAO.queryAllOrdersCount(year,month);
		List<Integer> counts=new ArrayList<Integer>();
		Map<Integer, OrdersCountBO> obmaps=ob.stream().collect(Collectors.toMap(OrdersCountBO::getDay, ordersCountBO -> ordersCountBO));
		
		String prefixStr=String.valueOf(year)+"-"+String.valueOf(month)+"-";
		for(int i=1;i<=daysCount;i++){
			item.add(prefixStr+String.valueOf(i));
			
			if(osmaps.containsKey(i)){
				Double money=osmaps.get(i).getMoney();
				moneys.add(money);
			}else{
				moneys.add(0.00);
			}
			
			if(obmaps.containsKey(i)){
				int count=obmaps.get(i).getCount();
				counts.add(count);
			}else{
				counts.add(0);
			}
		}
		
		OrdersInfoVO ordersInfoVO=new OrdersInfoVO();
		
		List<Object> data=new ArrayList<Object>();
		data.add(moneys);
		data.add(counts);
		
		ordersInfoVO.setData(data);
		ordersInfoVO.setItem(item);
		
		result.setData(ordersInfoVO);
		
		return result;
	}

	@Override
	public ResultVO queryForGoods(String sessionId,int month){
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		List<GoodsCountBO> data=homeDAO.queryForGoods(shop.getId(), month);
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO queryInternalNotice(){
		ResultVO result=new ResultVO();
		List<NoticeBO> data=homeDAO.queryInternalNotice();
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO queryInternalShopNotice(){
		ResultVO result=new ResultVO();
		List<NoticeBO> data=homeDAO.queryInternalShopNotice();
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO queryTotal(String sessionId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String chainId = systemUserSessionVO.getSystemId();
		HomeHeaderInfoBO homeHeaderInfoBO = payHistoryDAO.getShopData(chainId);
		homeHeaderInfoBO.setTotalCount(chainDAO.queryTotal(chainId));
		homeHeaderInfoBO.setTotalOrders(ordersDAO.getTotal(chainId));
		result.setData(homeHeaderInfoBO);
		return result;
	}

	@Override
	public ResultVO querySsoGoods(HomeTableVO homeTableVO) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(homeTableVO.getSessionId()), SystemUserSessionVO.class);
		String chainId = systemUserSessionVO.getSystemId();
		HomeTableBO homeTableBO = new HomeTableBO();
		BeanUtils.copyProperties(homeTableVO, homeTableBO);
		homeTableBO.setChainId(chainId);
		result.setData(homeDAO.querySsoGoods(homeTableBO));
		return result;
	}

	@Override
	public ResultVO queryShopIncome(HomeTableVO homeTableVO) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(homeTableVO.getSessionId()), SystemUserSessionVO.class);
		String chainId = systemUserSessionVO.getSystemId();
		HomeTableBO homeTableBO = new HomeTableBO();
		BeanUtils.copyProperties(homeTableVO, homeTableBO);
		homeTableBO.setChainId(chainId);
		result.setData(homeDAO.queryShopIncome(homeTableBO));
		return result;
	}

	@Override
	public ResultVO queryAllShop(String sessionId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String chainId = systemUserSessionVO.getSystemId();
		result.setData(homeDAO.queryAllShop(chainId));
		return result;
	}

}
